package se.webbzon.boltzmann.minigame.animation;

import se.webbzon.boltzmann.minigame.sandbox.AnimationListener;
import se.webbzon.boltzmann.minigame.sandbox.DefaultSandBoxAnimation;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxAnimatable;
import se.webbzon.boltzmann.minigame.sandbox.SandBoxAnimation;

public class AndAnimation extends DefaultSandBoxAnimation {
	
	// The animations of this animation collection
	private final SandBoxAnimation[] animations;
	
	// The number of completed animations 
	private int notified;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public AndAnimation(SandBoxAnimation...animations) {
		AnimationListenerImpl listener = new AnimationListenerImpl();
		this.animations = animations;
		for (SandBoxAnimation animation : animations)
			animation.addListener(listener);
		notified = 0;
	}
	
	/*============================================================
	Public Methods
	============================================================*/

	@Override public void onStepEnd(SandBoxAnimatable picture) {
		for (SandBoxAnimation animation : animations)
			animation.onStepEnd(picture);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private final class AnimationListenerImpl implements AnimationListener {

		@Override public void onComplete(SandBoxAnimation animation) {
			notified++;
			animation.removeListener(this);
			if (notified == animations.length) {
				notifyComplete();
			}
		}
		
	}

}
